package cate.game.common.msg;

import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBCommon;
import cp.solution.business.sys.app.GameAppEntity;
import easy.java.dev.note.NoteField;
import org.apache.commons.codec.digest.DigestUtils;

@ClientMsg(title = "处理key の 请求")
public class HandleKeyReq implements MsgInput {

    @NoteField(value = "GameId")
    public String gameId;

    @NoteField(value = "待处理的内容", detail = "替换关键词 #GameKey# 和 #SecretKey#", example = "a=123&b=XYZ&gameKey=#GameKey#&sk=#SecretKey#")
    public String input;

    @NoteField(value = "前端自由使用的上下文", example = "funcA")
    public String context;

    @Override
    public void execute(GameBody game, ClientContext cc) {
        HandleKeyResp resp = new HandleKeyResp();

        GameAppEntity app = game.apisLoader().console.gameApp.getByCode(this.gameId);
        if(app == null) {
            game.notice.message(cc.getRole(), "GameID:"+this.gameId+" not found");
            return;
        }
        String output = this.input.replaceAll("#GameKey#", app.getGameKey());
        output = output.replaceAll("#SecretKey#", app.getSecretKey());
        resp.output = DigestUtils.md5Hex(output);
        resp.context = this.context;
        cc.sendNow(resp);

    }

    @Override
    public MsgConfig conf() {
        return MsgIdBCommon.HANDLE_KEY_REQ;
    }
}